//
//  ViewController.m
//  COJoke
//
//  Created by 陈渊佑 on 14/12/16.
//  Copyright (c) 2014年 carlosk. All rights reserved.
//

#import "ArticleListVC.h"
#import "NewModel.h"
#import "NewCell.h"
#import "WebVC.h"
@interface ArticleListVC ()
@property(nonatomic,strong)IBOutlet UITableView *tableV;
@property(nonatomic,strong)NSMutableArray  * datas;
@property(nonatomic,assign)int maxTimes;

@end

@implementation ArticleListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [COAppTool appName];
    self.datas = [NSMutableArray array];
    self.tableV.frame = self.view.frame;
    self.tableV.height = kFBaseHeightNoStatusNoNav;
    self.tableV.y = kFBaseNavHeight + kFBaseStatusHeight;
    //显示
    [self.tableV fillWithItems:self.datas withFillCellBlock:nil withCOBaseCellClass:[NewCell class]];
    __block typeof(self)bSelf = self;

    [self.tableV setOnClickCellBlock:^(NSIndexPath *indexPath, NewModel *item) {
        WebVC *vc = [[WebVC alloc] init];
        vc.model = item;
        [bSelf.navigationController pushViewController:vc animated:YES];
    }];

}
//#define openId @"oIWsFt56rmZK7XyZzjFm9QOYz3Og"
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if(self.datas.count == 0){
        self.maxTimes = 0;
        [self doGetArcList];
    }
}
- (void)doGetArcList{
    self.maxTimes ++;
    if (self.maxTimes > 10) {
        self.maxTimes = 0;
        return;
    }
    __block typeof(self)bSelf = self;
    
    NSBundle*bundle =[NSBundle mainBundle];
    NSDictionary*info =[bundle infoDictionary];
    NSString*openId =[info objectForKey:@"OpenID"];

    [HttpService doGetArticleList:openId withBlock:^(NSArray *articleList) {
        NSLog(@"result = %@",articleList);
        if (articleList == nil) {
            [self doGetArcList];
            return ;
        }
        [bSelf.datas removeAllObjects];
        [bSelf.datas addObjectsFromArray:articleList];
        [bSelf.tableV reloadData];
    }];
}

@end
